namespace ACRSAP.Administration.Projects
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ProjectList : Page
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("ProjectsGrid")]
        private DataGrid _ProjectsGrid;
        private object designerPlaceholderDeclaration;

        public ProjectList()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
        }

        public void AddProject(object s, EventArgs e)
        {
            this.Response.Redirect("AddProject.aspx");
        }

        private void BindProjects()
        {
            ProjectCollection projectList = Project.GetAllProjects();
            this.SortGridData(projectList, this.SortField, this.SortAscending);
            this.ProjectsGrid.DataSource = projectList;
            this.ProjectsGrid.DataBind();
        }

        public void CloneProject(object s, EventArgs e)
        {
            this.Response.Redirect("CloneProject.aspx");
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.BindProjects();
            }
        }

        protected void ProjectsGrid_Page(object sender, DataGridPageChangedEventArgs e)
        {
            this.ProjectsGrid.CurrentPageIndex = e.NewPageIndex;
            this.BindProjects();
        }

        private void ProjectsGrid_Sort(object s, DataGridSortCommandEventArgs e)
        {
            this.SortField = e.SortExpression;
            this.BindProjects();
        }

        private void SortGridData(ProjectCollection list, string sortField, bool asc)
        {
            ProjectCollection.ProjectFields sortCol = ProjectCollection.ProjectFields.InitValue;
            string _Vb_t_string_0 = sortField;
            if (StringType.StrCmp(_Vb_t_string_0, "Name", false) == 0)
            {
                sortCol = ProjectCollection.ProjectFields.Name;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Manager", false) == 0)
            {
                sortCol = ProjectCollection.ProjectFields.Manager;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "Creator", false) == 0)
            {
                sortCol = ProjectCollection.ProjectFields.Creator;
            }
            else if (StringType.StrCmp(_Vb_t_string_0, "DateCreated", false) == 0)
            {
                sortCol = ProjectCollection.ProjectFields.DateCreated;
            }
            list.Sort(sortCol, asc);
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddProject);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddProject);
                }
            }
        }

        protected virtual DataGrid ProjectsGrid
        {
            get
            {
                return this._ProjectsGrid;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._ProjectsGrid != null)
                {
                    this._ProjectsGrid.SortCommand -= new DataGridSortCommandEventHandler(this.ProjectsGrid_Sort);
                    this._ProjectsGrid.PageIndexChanged -= new DataGridPageChangedEventHandler(this.ProjectsGrid_Page);
                }
                this._ProjectsGrid = value;
                if (this._ProjectsGrid != null)
                {
                    this._ProjectsGrid.SortCommand += new DataGridSortCommandEventHandler(this.ProjectsGrid_Sort);
                    this._ProjectsGrid.PageIndexChanged += new DataGridPageChangedEventHandler(this.ProjectsGrid_Page);
                }
            }
        }

        public bool SortAscending
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortAscending"]);
                return ((o == null) || BooleanType.FromObject(o));
            }
            set
            {
                this.ViewState["SortAscending"] = value;
            }
        }

        public string SortField
        {
            get
            {
                object o = RuntimeHelpers.GetObjectValue(this.ViewState["SortField"]);
                if (o == null)
                {
                    return string.Empty;
                }
                return StringType.FromObject(o);
            }
            set
            {
                if (StringType.StrCmp(value, this.SortField, false) == 0)
                {
                    this.SortAscending = !this.SortAscending;
                }
                this.ViewState["SortField"] = value;
            }
        }
    }
}

